package com.brightfuture.chrms.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.brightfuture.chrms.entity.PersonAttendanceEntity;
import com.brightfuture.chrms.entity.PersonEntity;
import com.brightfuture.chrms.mapper.PersonAttendanceMapper;
import com.brightfuture.chrms.mapper.PersonMapper;
import com.brightfuture.chrms.service.PersonService;
import org.springframework.stereotype.Service;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.brightfuture.chrms.entity.PersonPositionEntity;
import com.brightfuture.chrms.mapper.PersonPositionMapper;
import org.springframework.transaction.annotation.Transactional;
import lombok.RequiredArgsConstructor;

import java.util.Objects;

/**
 * 人员;人员表
 *
 * @author Victor
 * @date 2024-02-03 17:00:42
 */
@Service
@RequiredArgsConstructor
public class PersonServiceImpl extends ServiceImpl<PersonMapper, PersonEntity> implements PersonService {
    private final PersonPositionMapper personPositionMapper;
    private final PersonAttendanceMapper personAttendanceMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveDeep(PersonEntity person) {
        baseMapper.insert(person);
        for (PersonPositionEntity personPosition : person.getPersonPositionList()) {
            personPosition.setPerId(person.getPerId());
            personPositionMapper.insert(personPosition);
        }
        for (PersonAttendanceEntity personAttendance : person.getPersonAttendanceList()) {
            personAttendance.setPerId(person.getPerId());
            personAttendanceMapper.insert(personAttendance);
        }

        return Boolean.TRUE;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateDeep(PersonEntity person) {
        baseMapper.updateById(person);
        for (PersonPositionEntity personPosition : person.getPersonPositionList()) {
            if (Objects.isNull(personPosition.getPerId())) {
                personPosition.setPerId(person.getPerId());
                personPositionMapper.insert(personPosition);
            } else {
                personPositionMapper.updateById(personPosition);
            }
        }
        for (PersonAttendanceEntity personAttendance : person.getPersonAttendanceList()) {
            if (Objects.isNull(personAttendance.getPerId())) {
                personAttendance.setPerId(person.getPerId());
                personAttendanceMapper.insert(personAttendance);
            } else {
                personAttendanceMapper.updateById(personAttendance);
            }
        }

        return Boolean.TRUE;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean removeDeep(Long[] ids) {
        baseMapper.deleteBatchIds(CollUtil.toList(ids));
        personPositionMapper.delete(Wrappers.<PersonPositionEntity>lambdaQuery().in(PersonPositionEntity::getPerId, ids));
        personAttendanceMapper.delete(Wrappers.<PersonAttendanceEntity>lambdaQuery().in(PersonAttendanceEntity::getPerId, ids));
        return Boolean.TRUE;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean removeChild(Long[] ids) {
        personPositionMapper.deleteBatchIds(CollUtil.toList(ids));
        personAttendanceMapper.deleteBatchIds(CollUtil.toList(ids));
        return Boolean.TRUE;
    }
}